package com.songshuai.webapi.login.service.impl;

import com.songshuai.webapi.login.dao.YxUsersMapper;
import com.songshuai.webapi.login.dto.UserLoginDto;
import com.songshuai.webapi.login.model.YxUsers;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

public class BaseLoginService {

    @Resource
    protected YxUsersMapper usersMapper;

    //permission
    UserLoginDto getYxUser(Integer integer, String username){
        Example example=new Example(YxUsers.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("isDelete",0);
        criteria.andEqualTo("isLeave",0);
        UserLoginDto userLoginDto=null;
        List<YxUsers> users=new ArrayList<>();
        if (1 == integer) {//电话
            criteria.andEqualTo("mobile", username);
            users = usersMapper.selectByExample(example);
            if (users.size() > 0) {
                userLoginDto = setUserProperties(users, username);

            }
        } else if (2 == integer) {//email
            criteria.andEqualTo("email", username);
            users = usersMapper.selectByExample(example);
            if (users.size() > 0) {
                userLoginDto = setUserProperties(users, username);
            }
        }else{
            return  userLoginDto;
        }

        return userLoginDto;
    }
    //crm
    UserLoginDto getYxUser(Integer integer, String username,Integer type){
        Example example = new Example(YxUsers.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("isDelete", 0);
        criteria.andEqualTo("isLeave", 0);
        if(1==type) {
            UserLoginDto userLoginDto = null;
            List<YxUsers> users = new ArrayList<>();
            if (1 == integer) {//电话
                criteria.andEqualTo("mobile", username);
                users = usersMapper.selectByExample(example);
                if (users.size() > 0) {
                    userLoginDto = setUserProperties(users, username);

                }
            } else {//username
                criteria.andEqualTo("userName", username);
                users = usersMapper.selectByExample(example);
                if (users.size() > 0) {
                    userLoginDto = setUserProperties(users, username);
                }
            }
            return userLoginDto;
        }else if(2==type){
            UserLoginDto userLoginDto = null;
            List<YxUsers> users = new ArrayList<>();
            if (1 == integer) {//电话
                criteria.andEqualTo("mobile", username);
                users = usersMapper.selectByExample(example);
                if (users.size() > 0) {
                    userLoginDto = setUserProperties(users, username);
                }
            }
            return userLoginDto;
        }else{
            return null;
        }
    }

    UserLoginDto setUserProperties(List<YxUsers> users,String username){
        YxUsers  yxUsers = users.get(0);
        UserLoginDto user=new UserLoginDto();
        user.setUsername(username);
        user.setPassword(yxUsers.getPassword());
        user.setId(Long.valueOf(yxUsers.getId()));
        return user;
    }
}
